﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Ace.Web.Helpers;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface IShopService : IAppService
    {
        List<Shop> GetList(int BrandID = 0, string keyword = "");

        List<ShopSample> GetShopList(int ProvinceID, int BrandID, string Key = "");

        PagedData<ShopSample> GetShopPageList(Pagination page, int ProvinceID, int BrandID, string Key = "");

        void Add(AddShopInput input);
        void Update(UpdateShopInput input);
        void Delete(int id );

        Shop GetModel(int Id);

        PagedData<Shop> GetPageData(Pagination page, int BrandID,int ProvinceID, string keyword);

        List<SimpleShop> GetCacheList();

        List<SimpleShop2> GetCacheList2();
    }

    public class ShopService : AppServiceBase<Shop>, IShopService
    {
        public ShopService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }


        public List<SimpleShop> GetCacheList()
        {

            /*
            List<SimpleShop> list = new List<SimpleShop>();
            
            MemoryCacheHelper mc = new MemoryCacheHelper();
            object obj = mc.Get(Ace.Globals.CacheShop);
            if (obj != null)
            {
                list = obj as List<SimpleShop>;
            }
            else
            {
                list = this.DbContext.Query<Shop>().Select(a => new SimpleShop() { ShopID = a.ID, ShopUrl = a.ShopUrl }).ToList(); 
                mc.Set(Ace.Globals.CacheShop, list, System.TimeSpan.FromMinutes(30), true);//缓存30分钟 自动延长
            }
            */
            List<SimpleShop> list = this.DbContext.Query<Shop>().Select(a => new SimpleShop() { ShopID = a.ID, ShopUrl = a.ShopUrl }).ToList();
            return list;
        }

        public List<SimpleShop2> GetCacheList2()
        {
            List<SimpleShop2> list = new List<SimpleShop2>();
            MemoryCacheHelper mc = new MemoryCacheHelper();
            object obj = mc.Get(Ace.Globals.CacheShop2);
            if (obj != null)
            {
                list = obj as List<SimpleShop2>;
            }
            else
            {
                list = this.DbContext.Query<Shop>().Select(a => new SimpleShop2() { ShopID = a.ID, ShopName = a.ShopName }).ToList();
                mc.Set(Ace.Globals.CacheShop2, list, System.TimeSpan.FromMinutes(30), true);//缓存30分钟 自动延长
            }
            return list;
        }


        public List<Shop> GetList(int BrandID = 0, string keyword = "")
        {
            var q = this.Query;

            if (BrandID > 0)
            {
                q = q.Where(a => a.BrandID == BrandID);
            }

            if (keyword.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.ShopName.Contains(keyword));
            }

            var ret = q.ToList();
            return ret;
        }

        public List<ShopSample> GetShopList(int ProvinceID, int BrandID, string Key = "")
        {
            //string sql = " select ID, ShopName, ShopAddress, ShopTel, ShopUrl, Distance, GPS_X, GPS_Y from ";
            //sql += " ( ";
            //sql += "  SELECT ID, ShopName, ShopAddress, ShopTel, ShopUrl, f_distance(?x,?y, GPS_X, GPS_Y) as Distance, GPS_X, GPS_Y from Shop ";
            //sql += " ) aa order by Distance asc LIMIT 0,5 ";
            //DbParam[] dbParams = new DbParam[] {
            //    new DbParam("?x",GPS_X),
            //    new DbParam("?y",GPS_Y)
            //};
            string sql = "select ID, ShopName, ShopAddress, ShopTel, ShopUrl from shop where 1=1 ";
            if(ProvinceID>1)
            {
                sql += " and ProvinceID="+ ProvinceID;
            }
            if(BrandID>0)
            {
                sql += " and BrandID=" + BrandID;
            }
            if(!string.IsNullOrEmpty(Key))
            {
                sql += " and ShopName like '%"+ Key + "%'"; 
            }
            List<ShopSample> list= this.DbContext.SqlQuery<ShopSample>(sql).ToList();
            
            return list;
        }

        public PagedData<ShopSample> GetShopPageList(Pagination page, int ProvinceID, int BrandID, string Key = "")
        {
            string strWhere = " 1=1 ";
            if (ProvinceID > 1)
            {
                strWhere += " and ProvinceID=" + ProvinceID;
            }
            if (BrandID > 0)
            {
                strWhere += " and BrandID=" + BrandID;
            }
            if (!string.IsNullOrEmpty(Key))
            {
                strWhere += " and ShopName like '%" + Key + "%'";
            }

            DbParam _totalcount = new DbParam("?_totalcount", null, typeof(int)) { Direction = ParamDirection.Output };
            DbParam _pagecount = new DbParam("?_pagecount", null, typeof(int)) { Direction = ParamDirection.Output };

            DbParam[] dbs = new DbParam[] {
                new DbParam("?_fields", "ID, ShopName, ShopAddress, ShopTel, ShopUrl"),
                new DbParam("?_tables", "shop"),
                new DbParam("?_where",strWhere),
                new DbParam("?_orderby", "ID desc"),
                new DbParam("?_pageindex", page.Page),
                new DbParam("?_pageSize", page.PageSize),
                _totalcount,
                _pagecount
            };

            List<ShopSample> list = this.DbContext.SqlQuery<ShopSample>("sp_viewPage", System.Data.CommandType.StoredProcedure, dbs).ToList();

            PagedData<ShopSample> pagedData = new PagedData<ShopSample>();
            pagedData.CurrentPage = page.Page;
            pagedData.Models = list;
            pagedData.PageSize = page.PageSize;
            pagedData.TotalCount = Convert.ToInt32(_totalcount.Value);
            pagedData.TotalPage = Convert.ToInt32(_pagecount.Value);
            
            return pagedData;
        }

        public void Add(AddShopInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateShopInput input)
        {
            this.UpdateFromDto(input);
        }

        public Shop GetModel(int ID)
        {
            return  this.Query.Where(a => a.ID == ID).FirstOrDefault();
        }

        public void Delete(int ID)
        {
            this.DbContext.Delete<Shop>(a => a.ID == ID);
        }


        public PagedData<Shop> GetPageData(Pagination page,int BrandID,int ProvinceID, string keyword)
        {
            var q = this.DbContext.Query<Shop>();

            q = q.WhereIfNotNullOrEmpty(keyword, a => a.ShopName.Contains(keyword) );

            if(BrandID>0)
            {
                q = q.Where(a => a.BrandID == BrandID);
            }

            if(ProvinceID>1)
            {
                q = q.Where(a => a.ProvinceID == ProvinceID);
            }
           

            PagedData<Shop> pagedData = q.TakePageData(page);
            
            return pagedData;
        }

         



    }

}
